package cn.sz.dzy.cloud.controller;

import cn.sz.dzy.cloud.apis.PayFeignApi;
import cn.hutool.core.date.DateUtil;
import cn.sz.dzy.cloud.entities.PayDTO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import cn.sz.dzy.cloud.resp.ResultData;
import cn.sz.dzy.cloud.resp.ReturnCodeEnum;

/**
 * @program: SpringCloud2024
 * @description:
 * @author: DingZhenYun
 * @create: 2024-04-14 22:26
 **/
@RestController
@RequestMapping("/feign")
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping(value = "/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO)
    {
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        ResultData resultData = payFeignApi.addPay(payDTO);
        return resultData;
    }

    @GetMapping(value = "/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id)
    {
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData resultData = null;
        try
        {
            System.out.println("调用开始-----: "+ DateUtil.now());
            resultData = payFeignApi.getPayInfo(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("调用结束-----: "+ DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
        return resultData;
    }

    /** 操千曲而晓声，观千剑而后识器
     * openfeign天然支持负载均衡演示
     * @return
     */
    @GetMapping(value = "/pay/mylb")
    public String mylb()
    {
        return payFeignApi.mylb();
    }
}
